home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_4 / yow / yow.c < prev    next >
C/C++ Source or Header  |  1992-05-11  |  3KB  |  128 lines

  1. #include <stdio.h>
  2. #include <exec/memory.h>
  3.  
  4. /* #include <ctype.h> */
  5.  
  6. #define PATH_EXEC "YOW:"
  7.  
  8. /* zippy.c
  9.  *
  10.  * Print a quotation from Zippy the Pinhead.
  11.  * Qux <Kaufman-David@Yale> March 6, 1986
  12.  *
  13.  * Compiled for AmigaDOS by Joseph Hillenburg (jph@gnu.ai.mit.edu,
  14.  *                          dolmen!anaconda!jph@iuvax.cs.indiana.edu)
  15.  *
  16.  * Amiga Port History
  17.  *
  18.  * 1.00     May 11      Very first release...very weak...
  19.  * 1.01     May 11      :) Cleaned up source a little...
  20.  *
  21.  * I plan to add...
  22.  *  An Intutition version?
  23.  *
  24.  */
  25.  
  26. #define BUFSIZE  2000
  27. #define SEP      '\0'
  28. #define YOW_FILE "yow.lines"
  29.  
  30. static char *VersTag = "\0$VER: AmigaYow (Joseph Hillenburg) 1.01 (05/11/92)";
  31.  
  32. main (argc, argv)
  33.      int argc;
  34.      char *argv[];
  35. {
  36.   FILE *fp;
  37.   char file[BUFSIZ];
  38.   void yow();
  39. /*ULONG seconds;
  40.   ULONG micros;*/
  41.  
  42. /*  CurrentTime(seconds,micros); */
  43.  
  44.   if (argc > 1 && (!strcmp (argv[1], "-h") || !strcmp (argv[1], "-H") || !strcmp (argv[1], "-?")))
  45.     {
  46.     printf("yow [-v] [-h] [-?] [-f <filename>]\n");
  47.     exit(1);
  48.     }
  49.  
  50.   if (argc > 1 && !strcmp (argv[1], "-v"))
  51.     {
  52.     printf("AmigaYow 1.01 by Joseph Hillenburg, May 11 1992\n");
  53.     printf("                 (jph@gnu.ai.mit.edu)\n");
  54.     printf("                 (dolmen!anaconda!jph@iuvax.cs.indiana.edu)\n");
  55.     exit(1);
  56.     }
  57.  
  58.   if (argc > 2 && !strcmp (argv[1], "-f"))
  59.     strcpy (file, argv[2]);
  60.   else
  61.     sprintf (file, "%s%s", PATH_EXEC, YOW_FILE);
  62.  
  63.   if ((fp = fopen(file, "r")) == NULL) {
  64.     perror(file);
  65.     exit(1);
  66.   }
  67.  
  68.   /* initialize random seed -- used to use time + getpid()*/
  69.   srand((int) (((long *) time(0)) + AvailMem()/* + micros*/));
  70.  
  71.   yow(fp);
  72.   fclose(fp);
  73.   exit(0);
  74. }
  75.  
  76. void
  77. yow (fp)
  78.      FILE *fp;
  79. {
  80.   static long len = -1;
  81.   long offset;
  82.   int c, i = 0;
  83.   char buf[BUFSIZE];
  84.  
  85.   /* Get length of file, go to a random place in it */
  86.   if (len == -1) {
  87.     if (fseek(fp, 0, 2) == -1) {
  88.       perror("fseek 1");
  89.       exit(1);
  90.     }
  91.     len = ftell(fp);
  92.   }
  93.   offset = rand() % len;
  94.   if (fseek(fp, offset, 0) == -1) {
  95.     perror("fseek 2");
  96.     exit(1);
  97.   }
  98.  
  99.   /* Read until SEP, read next line, print it.
  100.      (Note that we will never print anything before the first seperator.)
  101.      If we hit EOF looking for the first SEP, just recurse. */
  102.   while ((c = getc(fp)) != SEP)
  103.     if (c == EOF) {
  104.       yow(fp);
  105.       return;
  106.     }
  107.  
  108.   /* Skip leading whitespace, then read in a quotation.
  109.      If we hit EOF before we find a non-whitespace char, recurse. */
  110.   while (isspace(c = getc(fp)))
  111.     ;
  112.   if (c == EOF) {
  113.     yow(fp);
  114.     return;
  115.   }
  116.   buf[i++] = c;
  117.   while ((c = getc(fp)) != SEP && c != EOF) {
  118.     buf[i++] = c;
  119.  
  120.     if (i == BUFSIZ-1)
  121.       /* Yow! Is this quotation too long yet? */
  122.       break;
  123.   }
  124.   buf[i++] = 0;
  125.   printf("%s\n", buf);
  126. }
  127.  
  128.